package dev.fastgql.integration;

import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.PubSecKeyOptions;
import io.vertx.ext.auth.jwt.JWTAuthOptions;
import io.vertx.ext.jwt.JWTOptions;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.ext.auth.jwt.JWTAuth;
import java.util.Map;

public interface WithSecurity extends WithFastGQL {

  String ALGORITHM = "RS256";
  String PUBLIC_KEY =
      "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvAOEBHuP9Pie9si3Ok3e/rBOYhyJC6jR+P6Np38biqxUbZRfvmXpS4sdsUIIiMP14C5sfzDtT+oe+FEOyleCjRY2aXCHUmqQHyxmUMe4sBvxn5wLoCDi528dpeDsq3QVvL+X0zgn266ZgCP2xwoXtc0ry8Y397jTonC90/jnawizn4rqm7s14ChPM1CUn5rzfGsd1/8BvyR3H7F2wGcgQlNYpKW8ge1YsvtmIzZswc6u2JWUg6wh1Is1BweBDULvJVONyCzyt97CyYp2Si5ISODrfe+VzYZnPfJ+mtuUQxa3+YivK1Hz0rFotK1cmDCpJ8AqfMKDnyh/urYWqAXFVQIDAQAB";
  String SECRET_KEY =
      "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC8A4QEe4/0+J72yLc6Td7+sE5iHIkLqNH4/o2nfxuKrFRtlF++ZelLix2xQgiIw/XgLmx/MO1P6h74UQ7KV4KNFjZpcIdSapAfLGZQx7iwG/GfnAugIOLnbx2l4OyrdBW8v5fTOCfbrpmAI/bHChe1zSvLxjf3uNOicL3T+OdrCLOfiuqbuzXgKE8zUJSfmvN8ax3X/wG/JHcfsXbAZyBCU1ikpbyB7Viy+2YjNmzBzq7YlZSDrCHUizUHB4ENQu8lU43ILPK33sLJinZKLkhI4Ot975XNhmc98n6a25RDFrf5iK8rUfPSsWi0rVyYMKknwCp8woOfKH+6thaoBcVVAgMBAAECggEAEwggEvwXBGsuXDjRI1vCmPFr3c6ZqM58AsJxwG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mPynA4IJ2vv29wDaQxTQhxbNNAkSclAcuundPX9KgjJDVBd/z8Srad5EJwVcPlJG57IF/RRVT75r2xoZQhYhbK+uIOZOKxqi";

  @Override
  default boolean isSecurityActive() {
    return true;
  }

  @Override
  default Map<String, Object> createSecurityConfigEntry() {
    return Map.of("algorithm", ALGORITHM, "publicKey", PUBLIC_KEY);
  }

  @Override
  default String getJwtToken(Vertx vertx, Map<String, Object> userParams) {
    JWTAuth jwtAuth =
        JWTAuth.create(
            vertx,
            new JWTAuthOptions()
                .addPubSecKey(
                    new PubSecKeyOptions()
                        .setAlgorithm(ALGORITHM)
                        .setPublicKey(PUBLIC_KEY)
                        .setSecretKey(SECRET_KEY)));

    return jwtAuth.generateToken(
        JsonObject.mapFrom(userParams), new JWTOptions().setAlgorithm(ALGORITHM));
  }
}
